package com.ocom.accesservice.controller;

import com.ocom.accesservice.service.AreaInfoService;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.constant.UserConstants;
import com.ocom.common.dto.AreaDto;
import com.ocom.common.entity.access.EntityMjAreaInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AreaInfo {
    @Autowired
    private AreaInfoService areaInfoService;


    /**
     * 添加区域
     * */
    @PostMapping(AccessServiceUrl.URL_NewArea)
    public Result NewArea(@Validated @RequestBody AreaDto areaDto){
        if (areaDto.getAreaName().equals("") || areaDto.getAreaName() == null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        EntityMjAreaInfo entityMjAreaInfo = areaInfoService.checkAreaNameUnique(areaDto.getComId(), areaDto.getAreaName());
        if (entityMjAreaInfo!=null && !entityMjAreaInfo.getAreaId().equals(areaDto.getAreaId())) {
            return ResultUtil.error(ResultEnum.Err_3000);
        }
        return ResultUtil.success(areaInfoService.NewArea(areaDto,areaDto.getComId()));
    }

    /**
     * 更新区域
     * */
    @PostMapping(AccessServiceUrl.URL_UpdateAreaName)
    public Result UpdateAreaName(@Validated @RequestBody AreaDto areaDto){
        if (areaDto.getAreaName().equals("") || areaDto.getAreaName() == null){
            return ResultUtil.error(ResultEnum.Err_203);
        }
        EntityMjAreaInfo entityMjAreaInfo = areaInfoService.checkAreaNameUnique(areaDto.getComId(), areaDto.getAreaName());
        if (entityMjAreaInfo!=null && !entityMjAreaInfo.getAreaId().equals(areaDto.getAreaId())) {
            return ResultUtil.error(ResultEnum.Err_3000);
        }
        return ResultUtil.success(areaInfoService.UpdateAreaName(areaDto,areaDto.getComId()));
    }


    /**获取区域信息*/
    @GetMapping(AccessServiceUrl.URL_GetAreaInfo)
    public Result GetAreaInfo(Long comId,Integer regionId,Integer areaId,String areaName){
        return ResultUtil.success(areaInfoService.GetAreaInfo(comId,regionId,areaId,areaName));
    }


    /**
     * 获取区域片区信息
     * */
    @PostMapping(AccessServiceUrl.URL_getAreaInfoByMachNo)
    public Result getAreaInfoByMachNo(Long companyId,String machNo){
        return ResultUtil.success(areaInfoService.getAreaInfoByMachNo(companyId,machNo));
    }

}
